package com.dson.code.controller;

import com.dson.common.LagouResponse;
import com.dson.common.service.AuthCodeService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/code")
public class AuthCodeController {

    @Reference
    private AuthCodeService authCodeService;

    @GetMapping("/create/{email}")
    public LagouResponse sendCode(@PathVariable("email") String email) {
        Boolean result = authCodeService.sendCode(email);
        if (Boolean.TRUE.equals(result)) {
            return LagouResponse.success("success");
        }
        return LagouResponse.failure("failure");
    }

    @GetMapping("/validate/{email}/{code}")
    public LagouResponse checkCode(@PathVariable("email") String email,
                                           @PathVariable("code") String code) {
        Integer result = authCodeService.checkCode(email, code);
        if (0 == result) {
            return LagouResponse.success("验证通过");
        }
        if (2 == result) {
            return LagouResponse.failure("验证码已过期");
        }
        return LagouResponse.failure("验证码错误");
    }
}
